"""
全国疫情地图构建
"""
import json

from pyecharts.charts import Map
from pyecharts.options import TitleOpts, VisualMapOpts

f = open('疫情.txt','r',encoding='utf-8')
data = f.read()
f.close()
data_dict = json.loads(data)
province_list = data_dict["areaTree"][0]["children"]
data_list = []
for province in province_list:
    if province["name"] in ["北京","上海","天津","重庆"]:
        name = province["name"] + "市"
    elif province["name"] == "新疆":
        name = "新疆维吾尔自治区"
    elif province["name"] == "内蒙古":
        name = "内蒙古自治区"
    elif province["name"] == "广西":
        name = "广西壮族自治区"
    elif province["name"] == "西藏":
        name = "西藏自治区"
    elif province["name"] == "宁夏":
        name = "宁夏回族自治区"
    elif province["name"] == "香港":
        name = "香港特别行政区"
    elif province["name"] == "澳门":
        name = "澳门特别行政区"
    else:
        name = province["name"] + "省"
    total = province["total"]["confirm"]
    data_list.append((name,total))
print(data_list)
# 构建地图
china_map = Map()
# 添加数据
china_map.add("确诊人数",data_list,"china")
# 设置全局选项
china_map.set_global_opts(
    title_opts=TitleOpts(title="全国疫情地图"),
    visualmap_opts=VisualMapOpts(
        is_show=True,#是否显示视觉映射组件
        is_piecewise=True,#是否分段显示
        pieces=[
            {"min":1,"max":9,"label":"1~9人","color":"#CCFFFF"},
            {"min":10,"max":99,"label":"10~99人","color":"#FFFF99"},
            {"min":100,"max":999,"label":"100~999人","color":"#FF9966"},
            {"min":1000,"max":9999,"label":"1000~9999人","color":"#FF6666"},
            {"min":10000,"max":100000,"label":"10000~99999人","color":"#CC3333"},
            {"min":100000,"label":"100000+人","color":"#990033"},
            ]
    ))
# 渲染地图
china_map.render("全国疫情地图.html")
